$(function () {
    $('#zyf_grid').datagrid({
        url: $.webPath() + "/organ/zyf/zyfList",
        toolbar: "#zyf_toolbar",
        fitColumns: true,
        singleSelect: true,
        pageNumber: 1,
        pagination: true,
        fit: true,
        rownumbers: "true",
        columns: [
            [
                {field: 'qc', title: '全称', width: 50},
                {field: 'jc', title: '简称', width: 30},
                {field: 'zyl', title: '质押率',
                    formatter:function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    },width: 30},
                {field: 'lxr', title: '联系人', width: 30},
                {field: 'lxdh', title: '联系电话', width: 50},
                {field: 'bm', title: '编码', width: 30},
                {field: 'bz', title: '备注', width: 60},
                {field: 'sfzx', title: '启用', align: 'center', width: 10, formatter: function (value) {
                    if (value == '是')
                        return '否';
                    else
                        return '是';
                }}
            ]
        ],
        onLoadSuccess: function () {
            $("#zyf_mod").hide();
            $("#zyf_del").hide();
            $("#zyf_start").hide();
        },
        onClickRow: function (rowIndex, rowData) {
            if (rowData.sfzx == null || rowData.sfzx == '是') {
                $("#zyf_mod").show();
                $("#zyf_del").show();
                $("#zyf_start").removeClass('stop').addClass('start');
                $("#zyf_start").text('启用');
                $('#zyf_start').unbind('click');
                $('#zyf_start').bind('click', function () {
                    changezyfState($.webPath() + '/organ/zyf/start_zyf', '确认启用此机构？');
                });
            } else {
                $("#zyf_mod").hide();
                $("#zyf_del").hide();
                $("#zyf_start").text('停用');
                $("#zyf_start").removeClass('start').addClass('stop');
                $('#zyf_start').unbind('click');
                $('#zyf_start').bind('click', function () {
                    changezyfState($.webPath() + '/organ/zyf/stop_zyf', '确认停用此机构？');
                });
            }
            $("#zyf_start").show();
        },
        onDblClickCell: function (rowIndex, field) {
            if (field == "qc")
                $("#zyf_mod:visible").click();
        },
        rowStyler: function (index, row) {
            if (row.sfzx == '是') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    });
    
    //**************按钮绑定事件*************
    //新增按钮触发
    $("#zyf_add").click(function () {
        zyf_newItem('新增质押方', 'add_zyf');
    });
    //修改按钮触发
    $("#zyf_mod").click(function () {
        qyfr_modify('修改质押方', 'update_zyf');
    });
    //删除按钮触发
    $('#zyf_del').click(
        zyf_deleteItem
    );
    //启用按钮触发
    $("#zyf_start").click(function () {
        changezyfState();
    });
    //搜索按钮触发
    $('#zyf_search').click(
        zyf_search
    );
    //**************按钮绑定事件*************
});

//新增质押方
function zyf_newItem(title, action) {
    var href = $.webPath() + "/organ/zyf/zyf_content";
    var actionUrl = $.webPath() + "/organ/zyf/" + action;
    zyf_showDialog(title, href, actionUrl);
}

//修改质押方
function qyfr_modify(title, action) {
    var record = $("#zyf_grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/organ/zyf/zyf_content?zyfId=" + record.id;
    var actionUrl = $.webPath() + "/organ/zyf/" + action;
    zyf_showDialog(title, href, actionUrl);
}

//新增或修改质押方弹出窗口
function zyf_showDialog(title, href, actionUrl) {
    var win = $.window({
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        },
        title: title,
        width: 600,
        height: 420,
        cache: false,
        iconCls: 'icon-add2',
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_zyf').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#zyf_grid').datagrid('reload');
                                $('#zyf_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除质押方
function zyf_deleteItem() {
    var zyfId = $("#zyf_grid").datagrid("getSelected").id;
    if (zyfId) {
        var url = $.webPath() + "/organ/zyf/delete_zyf?zyfId=" + zyfId;
        var message = "是否确认删除此质押方";
        $.messager.confirm('确认', message, function (r) {
            if (r) {
                $.post(url,{},function(data){
                    if (data.statusCode == 200) {
                        $.messager.show({
                            title: '提示框',
                            msg: data.message,
                            timeout: 3000,
                            showType: 'slide'
                        });
                        $('#zyf_grid').datagrid('reload');
                    } else {
                        $.messager.alert('提示', data.message, 'info');
                    }
                })
            }
        });
    }
}

//查询质押方
function zyf_search() {
    var qc = $('#_zyf').val() == null ? null : $('#_zyf').val();
    var jc = $('#zyf').val() == null ? null : $('#zyf').val();
    $('#zyf_grid').datagrid('load',
        {
            qc:qc,
            jc:jc
        }
    );
}

//改变状态，启用停用
function changezyfState(url, message) {
    var zyfId = $("#zyf_grid").datagrid("getSelected").id;
    if (zyfId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.post(url,{'zyfId':zyfId},function(data){
                    if (data.statusCode == 200) {
                        $('#zyf_grid').datagrid('reload');
                        $.messager.show({
                            title: '提示框',
                            msg: data.message,
                            timeout: 3000,
                            showType: 'slide'
                        });
                    } else {
                        $.messager.alert('提示', data.message, 'info');
                    }
                })

            }
        })
    }
}
